package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.pojo.dto.ApiTokenSaveDTO;
import com.xbongbong.paas.pojo.dto.WhiteListUpdateDTO;
import com.xbongbong.paas.pojo.vo.ApiTokenSaveVO;
import com.xbongbong.paas.pojo.vo.ApiTokenVO;
import com.xbongbong.paas.pojo.vo.TokenUpdateVO;
import com.xbongbong.paas.pojo.vo.WhiteListUpdateVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.DigestUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.ApiTokenService;
import com.xbongbong.sys.domain.entity.ApiTokenEntity;
import com.xbongbong.sys.model.ApiTokenModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *
 * 创建时间 2019/10/10 7:15 PM
 * 修改时间 2019/10/10 7:15 PM
 * @author chy
 * @since v1.1.3
 * @version v1.1.3
 */
@Service("apiTokenService")
public class ApiTokenServiceImpl implements ApiTokenService {

    @Resource
    private ApiTokenModel apiTokenModel;

    @Override
    public ApiTokenSaveVO save(ApiTokenSaveDTO apiTokenSaveDTO) throws XbbException {

        UserVO userVO = apiTokenSaveDTO.getLoginUser();
        if (!userVO.isAdminOrBoss()) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100017);
        }

        String corpid = apiTokenSaveDTO.getCorpid();
        String token = apiTokenSaveDTO.getToken();
        String whiteList = apiTokenSaveDTO.getWhiteList();

        ApiTokenEntity apiTokenEntity = apiTokenModel.getByCorpId(corpid);

        if (apiTokenEntity == null) {
            apiTokenEntity = new ApiTokenEntity();
            apiTokenEntity.setCorpid(corpid);
            apiTokenEntity.setToken("");
            apiTokenEntity.setWhiteList("");
        }

        if (!StringUtil.isEmpty(token)) {
            apiTokenEntity.setToken(token);
        }

        if (whiteList != null) {
            apiTokenEntity.setWhiteList(whiteList);
        }
        Integer id = apiTokenModel.save(apiTokenEntity);
        ApiTokenSaveVO apiTokenSaveVO = new ApiTokenSaveVO();
        apiTokenSaveVO.setId(id);
        return apiTokenSaveVO;
    }

    @Override
    public TokenUpdateVO tokenUpdate(BaseDTO baseDTO) throws XbbException {

        UserVO userVO = baseDTO.getLoginUser();
        if (!userVO.isAdminOrBoss()) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100017);
        }

        String corpid = baseDTO.getCorpid();

        //规则是公司ID + pro + 时间戳 字符串进行SHA256 然后取前32位
        String plainText = corpid + "pro" + DateUtil.getInt();
        String newToken = DigestUtil.Encrypt(plainText, "SHA-256").substring(0, 32);
        ApiTokenSaveDTO apiTokenSaveDTO = new ApiTokenSaveDTO();

        BeanUtil.copyProperties(baseDTO, apiTokenSaveDTO);

        apiTokenSaveDTO.setToken(newToken);

        save(apiTokenSaveDTO);

        TokenUpdateVO tokenUpdateVO = new TokenUpdateVO();
        tokenUpdateVO.setToken(newToken);
        return tokenUpdateVO;
    }

    @Override
    public WhiteListUpdateVO whiteListUpdate(WhiteListUpdateDTO whiteListUpdateDTO) throws XbbException {

        UserVO userVO = whiteListUpdateDTO.getLoginUser();
        if (!userVO.isAdminOrBoss()) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100017);
        }

        ApiTokenSaveDTO apiTokenSaveDTO = new ApiTokenSaveDTO();

        BeanUtil.copyProperties(whiteListUpdateDTO, apiTokenSaveDTO);
        ApiTokenSaveVO apiTokenSaveVO = save(apiTokenSaveDTO);

        WhiteListUpdateVO whiteListUpdateVO = new WhiteListUpdateVO();
        whiteListUpdateVO.setId(apiTokenSaveVO.getId());
        return whiteListUpdateVO;
    }

    @Override
    public ApiTokenVO getApiToken(BaseDTO baseDTO) throws XbbException {

        UserVO userVO = baseDTO.getLoginUser();
        if (!userVO.isAdminOrBoss()) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100017);
        }

        ApiTokenVO apiTokenVO = new ApiTokenVO();
        String corpid = baseDTO.getCorpid();
        ApiTokenEntity apiTokenEntity = apiTokenModel.getByCorpId(corpid);

        if (apiTokenEntity != null) {
            apiTokenVO.setCorpid(corpid);
            apiTokenVO.setUserId(baseDTO.getUserId());
            apiTokenVO.setToken(apiTokenEntity.getToken());
            apiTokenVO.setWhiteList(apiTokenEntity.getWhiteList());
        } else {
            apiTokenVO.setCorpid(corpid);
            apiTokenVO.setUserId(baseDTO.getUserId());
        }
        return apiTokenVO;
    }

}
